const { contextBridge, ipcRenderer } = require("electron");
const download = require('./plugin-dowmload');
const path = require('path');
const fs = require('fs');
const isPack = ipcRenderer.sendSync('get-package-status')


let servicePath = {};
try {
    servicePath = JSON.parse(fs.readFileSync(path.join(__dirname, (isPack ? '../../../' : './') + 'servicePath.json'), 'utf-8'));
} catch (error) {
    fs.writeFileSync(path.join(__dirname, (isPack ? '../../../' : '../') + 'servicePath.json'), `{
    "service": "http://192.168.3.106:88/PowerTool/"
}`);
    servicePath = {
        "service": "http://192.168.3.106:88/PowerTool/"
    }
}



contextBridge.exposeInMainWorld("EVENT_APP", {
    download: (FileName, key, id, callback) => {
        id && ipcRenderer.send('close-plug-in', id);
        var _download = new download();
        _download.install(FileName, key, callback)
    },
    getServicePath: () => {
        return servicePath.service;
    },
    getApps: (callback) => {
        var apps = ipcRenderer.sendSync('getApps');
        callback(apps);
    },
    runApp: (detail) => {
        ipcRenderer.send('open-plug-in', detail);
    },
});